if (!my.canIUse('plugin') && !my.isIDE) {
  my.ap && my.ap.updateAlipayClient && my.ap.updateAlipayClient();
}
import { grantAuth,extensionStatistics } from "./api";
App({
  globalData: {
    orderFrom: '',
    jumpData: {},
    sale_user_id:'',
    phone_brand:'',
    cateId:'',

  },
  onLaunch(options) {
    // 第一次打开
    console.log('App onLaunch Scene:', options.scene); //options.scene 是 String 类型的
    my.setStorageSync({key:'scene',data:options.scene});
    const token = my.getStorageSync({ key: 'token' }).data;
    if (!token) {
      this.getAuthBase();
    }
  },
  onShow(options) {
    my.removeStorage({ key: 'isAuthShow' });
    const updateManager = my.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      if (res.hasUpdate) {
        updateManager.onUpdateReady(function () {
          my.confirm({
            title: '更新提示',
            content: '新版本已发布，是否重启应用？',
            success: function (res) {
              if (res.confirm) {
                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                updateManager.applyUpdate()
              }
            }
          })
        })
      }
    })
    const that = this
    console.log(options.query);
    if (options.query) {
      let queryData = JSON.parse(JSON.stringify(options.query))
      that.globalData.orderFrom = queryData.from || ''
      that.globalData.jumpData = queryData
      that.globalData.sale_user_id=queryData.saleuserid
      that.globalData.cateId=queryData.id
      
    }
    if (options.referrerInfo) {
      let queryData = options.referrerInfo.extraData
      that.globalData.orderFrom = queryData
      that.globalData.jumpData = queryData
      that.globalData.cateId=queryData.id
    }
  },
  // 静默授权
  getAuthBase() {
    const that = this
    my.getAuthCode({
      scopes: 'auth_base',
      success: (res) => {
        if (res.authCode) {
          const code = res.authCode.split('&')[0];
          grantAuth({
            sale_user_id:that.globalData.sale_user_id,
            authCode: res.authCode,
            type: 1
          }).then(res1 => {
            my.setStorage({ key: 'token', data: res1.token });
            my.setStorage({ key: 'alipayUid', data: res1.alipayUid });
          })
        }
      }, fail: (res) => {
      }
    });
  },
  // 主动授权
  getAuthUser(callback) {
    const that = this;
    my.getSystemInfo({
      success: (res) => {
        that.globalData.phone_brand=res.brand+','+res.model;
      }
    })

  
    my.getAuthCode({
      scopes: 'auth_user',
      success: (res) => {
        if (res.authCode) {
          const code = res.authCode.split('&')[0];
          grantAuth({
            sale_user_id:that.globalData.sale_user_id,
            authCode: res.authCode,
            type: 2,
            equipment:that.globalData.phone_brand
          }).then(res1 => {
            if (res1) {
              my.setStorage({ key: 'token', data: res1.token });
              my.setStorage({ key: 'alipayUid', data: res1.alipayUid });
              my.setStorage({
                key: 'userInfo',
                data: { avatar: res1.avatar, nickName: res1.nickName }
              });
              my.showToast({
                type: 'success',
                content: '授权成功',
              })
              return callback(true);
            } else {
              return callback(false);
            }
          })
        }
      }, fail: (res) => {
        my.confirm({
          title: '提示',
          content: '取消授权，可能会使部分服务无法使用，或页面信息不完整',
          confirmButtonText: '重新授权',
          cancelButtonText: '我知道了',
          success: (result) => {
            if (result.confirm) {
              that.getAuthUser(callback);
            }
          },
        });
        return callback(false);
      }
    });
  }
});
